# ------------------------------------------------
#
# @file Makefile (based on gcc)
# @author Dewin
# @version v1.0.0
#
# ChangeLog :
#   2021-11-20
# ------------------------------------------------

######################################
# target
######################################
TARGET = Bootloader

######################################
# building variables
######################################
# debug build
ifeq ($(release), y)
DEBUG = 0
else
DEBUG = 1
endif
# optimization
OPT = -Os


USER_BOOTLOADER_CODE = yes

#######################################
# Build path
#######################################
BUILD_DIR = build

######################################
# chip platform info
######################################
TARGET_PLATFORM := n32g430
DEFS += -DN32G430
DEFS += -DUSE_STDPERIPH_DRIVER


######################################
#TOOLS CHAIN
######################################
CROSS_COMPILE = arm-none-eabi-

######################################
# C sources
######################################
C_DIRS += Core/Src
C_DIRS += User/Usarts
C_DIRS += Driver/CMSIS/device
C_DIRS += Driver/$(TARGET_PLATFORM)_std_periph_driver/src
C_DIRS += ../Common/Led
C_DIRS += ../Common/Debug
C_DIRS += ../Common/Usart
C_DIRS += ../Common/Flash
C_DIRS += ../Common/Settings
C_DIRS += ../Common/Log
SRC_OBJS_DIRS += $(foreach DIR, $(C_DIRS), $(wildcard $(DIR)/*.c))
C_SOURCES = $(SRC_OBJS_DIRS)
# C_DIRS += ../Common/Flash
######################################
# ASM sources
######################################
ASM_SOURCES = Driver/CMSIS/device/startup/startup_$(TARGET_PLATFORM)_gcc.s

######################################
# C includes
######################################
C_INCS += Core/Inc
C_INCS += User/Usart
C_INCS += ../Common
C_INCS += ../Common/Led
C_INCS += ../Common/Version
C_INCS += ../Common/Debug
C_INCS += ../Common/Usart
C_INCS += ../Common/Flash
C_INCS += ../Common/Settings
C_INCS += ../Common/Log
C_INCS += Driver/CMSIS/core
C_INCS += Driver/CMSIS/device
C_INCS += Driver/$(TARGET_PLATFORM)_std_periph_driver/inc
INCS_OBJS_DIR = $(foreach DIR2, $(C_INCS), $(wildcard $(DIR2)/*.h))
INCS_OBJS_PATH = $(sort $(dir $(INCS_OBJS_DIR)))
C_INCLUDES = $(addprefix -I,$(INCS_OBJS_PATH))

######################################
# Compile & Link flags
######################################
# cpu
CPU = -mcpu=cortex-m4
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

#CFLAGS
CFLAGS += $(MCU) -Wall
CFLAGS += $(OPT)
CFLAGS += -ffunction-sections -fdata-sections
#DEBUG
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif

ifeq ($(USER_BOOTLOADER_CODE), yes)
CFLAGS += -DUSER_BOOTLOADER_CODE
endif

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
#LFLAGS
# LFLAGS += $(MCU)
# LFLAGS += -Wl,--gc-sections 
# LFLAGS += -specs=nano.specs
# LFLAGS += -Xlinker -Map=$(BUILD_DIR)/$(TARGET).map

LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

# add C_FLAG By Max

CFLAGS += -Wunused-but-set-variable
CFLAGS += -Wno-unused-variable
CFLAGS += -Wno-unused-function
CFLAGS += -Wno-unused-but-set-variable
CFLAGS += -Wno-incompatible-pointer-types


# link script
LDSCRIPT = Driver/CMSIS/device/$(TARGET_PLATFORM)_flash.ld

######################################
# Objects
######################################
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CROSS_COMPILE)gcc $(CFLAGS) $(DEFS) $(C_INCLUDES) $(C_LIBS) -c -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
    
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(CROSS_COMPILE)gcc $(CFLAGS) -c $< -o $@
    
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CROSS_COMPILE)gcc $(OBJECTS) $(LDFLAGS) -o $@
	$(CROSS_COMPILE)size $@
	
$(BUILD_DIR)/$(TARGET).bin: $(BUILD_DIR)/$(TARGET).elf
	$(CROSS_COMPILE)objcopy -O binary -S $< $@

$(BUILD_DIR)/$(TARGET).hex: $(BUILD_DIR)/$(TARGET).elf
	$(CROSS_COMPILE)objcopy -O ihex -S $< $@
    
$(BUILD_DIR):
	@-mkdir $@

#SHELL=cmd.exe
#######################################
# clean up
#######################################
clean:
#	del $(BUILD_DIR)
	@-rm -rf $(BUILD_DIR)

#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)

# *** EOF ***